package com.yunhe.tianhe.rentcars.service.permission.impl;
import com.yunhe.tianhe.commons.dto.Pageable;
import com.yunhe.tianhe.commons.util.StringUtils;
import com.yunhe.tianhe.rentcars.dao.permission.RoleDao;
import com.yunhe.tianhe.rentcars.entity.permission.Role;
import com.yunhe.tianhe.rentcars.service.permission.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author lanyangyun
 * @version V1.0
 * @Description: //TODO
 * @date 2017/10/19 0019 下午 7:42
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public Role find(Long roleId) {
        return roleDao.find(roleId);
    }

    @Override
    public int findCount() {
        return 0;
    }

    @Override
    public List<Role> findList(Integer offset, Integer size) {
        return null;
    }

    @Override
    public void saveRolePermission(Long roleId, Long permisId) {

    }

    @Override
    public void saveRolePermissions(Integer[][] Integers) {

    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Long roleId) {
        return null;
    }

    @Override
    public void deletePermissionByRoleId(Long roleId) {

    }


    @Override
    public void save(Role role, String ids) {
        roleDao.save(role);
        Long roleId = role.getRoleId();
        Long permissionId = null;
        if (null!=roleId&& StringUtils.notEmpty(ids)){
            String[] permissionIds = ids.split(",");
           /* // 批量插入
            Integer[][] Integers = new Integer[permissionIds.length][2];
            for (int i = 0; i< permissionIds.length; i++){
                Integers[i][0] = roleId;
                Integers[i][1] = new Integer(permissionIds[i]);
            }
            roleDao.saveRolePermissions(Integers);*/
           for (int i = 0; i < permissionIds.length; i++){
              permissionId = new Long(permissionIds[i]);
               roleDao.saveRolePermission(roleId,permissionId);
           }
        }
    }

    @Override
    public void delete(Long roleId) {
        roleDao.delete(roleId);
        roleDao.deletePermissionByRoleId(roleId);
    }

    @Override
    public void deleteBatch(String ids) {
        if (StringUtils.notEmpty(ids)){
            String[] strings = ids.split(",");
            for (String s: strings){
                Long l = new Long(s);
                this.delete(l);
                roleDao.deletePermissionByRoleId(l);
            }
        }
    }

    @Override
    public void update(Role role,String ids) {
        roleDao.update(role);
        Long roleId = role.getRoleId();
        Long permissionId = null;
        if (null!=roleId&&StringUtils.notEmpty(ids)){
            roleDao.deletePermissionByRoleId(roleId);
            String[] permissionIds = ids.split(",");
            for (int i = 0; i < permissionIds.length; i++){
                permissionId = new Long(permissionIds[i]);
                roleDao.saveRolePermission(roleId,permissionId);
            }
        }
    }


    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }


    @Override
    public Pageable<Role> findPage(Integer offset, Integer size) {
        Pageable<Role> pageable = new Pageable();
        int count = roleDao.findCount();
        List<Role> data = null;
        if (count>0){
            pageable.setPage(offset/size+1);
            pageable.setSize(size);
            data = roleDao.findList(offset,size);
        }
        pageable.setTotal(new Long(count));
        pageable.setRows(data);
        return pageable;
//        return roleDao.findPage(offset,size);
    }

}
